/**************************************************************
 *@ProjectName: blweibo
 *@FileName: CommentBlog.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.zhiweibo.models.Comment;
import com.zhiweibo.models.StaticInfo;
import com.zhiweibo.util.Constant;
import com.zhiweibo.util.HttpUtil;

/**
 * The Class CommentBlog.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class CommentBlog extends BaseActivity implements OnItemClickListener {

    /** The lv cmt. */
    private ListView lvCmt;

    /** The str topic id. */
    private String strTopicId;

    /** The m list. */
    private List<Comment> mList;

    /** The m page. */
    private int mPage = 1;

    /** The adapter. */
    private WeiboCmtListAdapter adapter;

    /** The m back list. */
    private List mBackList = new ArrayList();

    /** The m comment. */
    private Comment mComment;

    /** The m select. */
    private int mSelect;

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#handleTitleBarEvent(int)
     */
    @Override
    protected void handleTitleBarEvent(int paramInt) {
        switch (paramInt) {
        case 0:
            Intent intent = new Intent(CommentBlog.this, EditActivity.class);
            intent.putExtra("mode", 1);
            intent.putExtra("topicId", strTopicId);
            startActivity(intent);
            break;
        case 1:
            finish();
            break;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setView(R.layout.commentblog);
        setTitleBar(1, getString(R.string.imageviewer_back), getString(R.string.main_comment),
                getString(R.string.itemmenu_comment));
        strTopicId = getIntent().getStringExtra("topicId");
        lvCmt = (ListView) findViewById(R.id.lvCmt);
        adapter = new WeiboCmtListAdapter();
        lvCmt.setAdapter(adapter);
        lvCmt.setOnItemClickListener(this);
        new FetchDataTask().execute(0);
    }

    /**
     * The Class FetchDataTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class FetchDataTask extends AsyncTask {

        /** The pd. */
        private ProgressDialog pd;

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected List doInBackground(Object... paramArrayOfInteger) {
            mList = HttpUtil.getComments(CommentBlog.this, Constant.COMMENTS + "?tid=" + strTopicId + "&count=50&page="
                    + mPage);
            return mList;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object paramList) {
            for (int i = 0; i < mList.size(); i++) {
                boolean b1 = true;
                Comment t1 = (Comment) mList.get(i);
                for (int j = 0; j < mBackList.size(); j++) {
                    Comment t2 = (Comment) mBackList.get(j);
                    if (t1.getCid() == t2.getCid()) {
                        b1 = false;
                        break;
                    }
                }
                if (b1) {
                    mBackList.add(t1);
                }
            }
            mList.clear();
            mList.addAll(mBackList);
            adapter.notifyDataSetChanged();
            if (pd != null) {
                pd.dismiss();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(CommentBlog.this);
            pd.setMessage("请稍候……");
            pd.show();
        }

    }

    /**
     * The Class WeiboCmtListAdapter.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class WeiboCmtListAdapter extends BaseAdapter {

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            if (mList == null || mList.size() == 0) {
                return 0;
            } else {
                return mList.size() + 1;
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int paramInt) {
            return paramInt;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int paramInt) {
            return paramInt;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getView(int, android.view.View,
         * android.view.ViewGroup)
         */
        @Override
        public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
            int j = mList.size();
            View localObject1;
            if (paramInt == j) {
                localObject1 = new TextView(CommentBlog.this);
                ((TextView) localObject1).setText(R.string.more);
                ((TextView) localObject1).setGravity(17);
                ((TextView) localObject1).setTextColor(Color.BLACK);
                localObject1.setPadding(15, 15, 15, 15);
            } else if (paramView == null) {
                localObject1 = new CommentMessageListItemView(CommentBlog.this, mList.get(paramInt));
            } else {
                try {
                    localObject1 = paramView;
                    try {
                        ((CommentMessageListItemView) paramView).update(CommentBlog.this, mList.get(paramInt));
                    } catch (Exception localException) {
                        localObject1 = new CommentMessageListItemView(CommentBlog.this, (Comment) mList.get(paramInt));
                    }
                } catch (Exception e) {
                    return null;
                }
            }
            return localObject1;
        }

        /**
         * Refresh.
         */
        public void refresh() {
            notifyDataSetChanged();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
     * .AdapterView, android.view.View, int, long)
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mList != null && mList.size() > 0 && position == mList.size()) {
            loadMore();
        } else {
            final List localList = new ArrayList();
            mSelect = position;
            mComment = mList.get(position);
            if (mComment.getUser().getUid().equals(StaticInfo.mUid)) {
                localList.add(getString(R.string.itemmenu_delete));
            } else if (mComment.getTopic().getUser().getUid().equals(StaticInfo.mUid)) {
                localList.add(getString(R.string.itemmenu_delete));
            }
            localList.add(getString(R.string.itemmenu_reply_comment));
            localList.add(getString(R.string.itemmenu_ori_mblog));
            localList.add(String.format(getString(R.string.view_one), mComment.getUser().getUsername()));
            showMenu(localList);
        }
    }

    /**
     * Load more.
     */
    private void loadMore() {
        mPage += 1;
        new FetchDataTask().execute(0);
    }

    /**
     * Show menu.
     * 
     * @param localList
     *            the local list
     */
    private void showMenu(final List localList) {
        AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        localBuilder.setTitle(R.string.item_menu_title);
        CharSequence[] arrayOfCharSequence1 = new CharSequence[0];
        CharSequence[] arrayOfCharSequence2 = (CharSequence[]) localList.toArray(arrayOfCharSequence1);
        localBuilder.setItems(arrayOfCharSequence2, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                String str = (String) localList.get(paramInt);
                doItemMenu(str);
            }
        });
        localBuilder.create().show();
    }

    /**
     * Do item menu.
     * 
     * @param paramString
     *            the param string
     */
    private void doItemMenu(String paramString) {
        if (paramString.equals(getString(R.string.itemmenu_reply_comment))) {
            Intent it = new Intent(this, EditActivity.class).putExtra("cid", mComment.getCid())
                    .putExtra("topicId", mComment.getTopic().getTid()).putExtra("mode", 5);
            if (!mComment.getUser().getUid().equals(StaticInfo.mUid)) {
                it.putExtra("content", "//@" + mComment.getUser().getUsername() + ":");

            }
            startActivityForResult(it, 100);
        } else if (paramString.equals(getString(R.string.itemmenu_delete))) {
            HttpUtil.getTopic(CommentBlog.this, Constant.COMMENT_DESTROY + mComment.getCid() + ".json");
            mList.remove(mSelect);
            adapter.notifyDataSetChanged();
        } else if (paramString.equals(getString(R.string.itemmenu_ori_mblog))) {
            startActivity(new Intent(this, DetailWeiboActivity.class).putExtra("topic", mComment.getTopic()));
        } else if (paramString.equals(String.format(getString(R.string.view_one), mComment.getUser().getUsername()))) {
            startActivity(new Intent(this, UserInfoActivity2.class).putExtra("user", mComment.getUser()));
        }
    }
}
